package cn.com.youyuan.ui;

import cn.com.youyuan.display.GraphicsProxy;
import cn.com.youyuan.models.UIContainer;
import cn.com.youyuan.models.UIElement;

public class Td extends List
{
	private int m_backgroundColor = 0xffffff;
	private int m_backgroundColorOnFocus = 0xffffff;
	private int m_borderColor = 0xffffff;
	private int m_borderColorOnFocus = 0xffffff;

	private int m_curLineHeight;

	public Td(UIContainer parent, int left, int top, int width, int height)
	{
		super(parent, left, top, width, height, false);
	}

	protected int getMinHeight()
	{
		// TODO Auto-generated method stub
		return 0;
	}

	public boolean canReceiveFocus()
	{
		for (int i = 0, j = getChildrenCount(); i < j; i++)
		{
			if (getChildAt(i).canReceiveFocus())
			{
				return true;
			}
		}
		return false;
	}

	public void setWidth(int width)
	{
		super.setWidth(width);
	}

	public void setLeft(int left)
	{
		//		System.out.println("TD set : L : " + left);
		super.setLeft(left);
	}

	public void setTop(int top)
	{
		System.out.println("TD set : T : " + top);
		int difference = top - getTop();
		super.setTop(top);
		UIElement uie = null;
		for (int i = 0, j = getChildrenCount(); i < j; i++)
		{
			uie = getChildAt(i);
			uie.setTop(uie.getTop() + difference);
		}
	}

	public void addChild(UIElement uie, boolean intoNextLine)
	{
		if (getFocusedItem() == null && uie.canReceiveFocus())
		{
			setFocusedItem(uie);
			((Tr) getParent()).updateFocus(this);
		}

		super.addChild(uie);

		if (!intoNextLine)
		{
			setTotalElementsHeight(getTotalElementsHeight() - uie.getHeight());
			if (uie.getHeight() > m_curLineHeight)
			{
				int difference = uie.getHeight() - m_curLineHeight;
				m_curLineHeight = uie.getHeight();
				setTotalElementsHeight(getTotalElementsHeight() + difference);
			}
		}

		setHeight(getTotalElementsHeight());
		getParent().onChildHeightChange(this, getHeight());
	}

	public void addChild(UIElement uie)
	{
		throw new IllegalArgumentException();
	}

	public boolean paint(GraphicsProxy g, int offsetHeight, boolean isFocused)
	{
		//		log(getClass(), "TD Paint : L : " + getLeft() + " T : " + getTop() + " W : " + getWidth() + " H : " + getHeight());
		//		g.setClip(getLeft() - 1, getTop() - 1 + offsetHeight, getWidth() + 2, getHeight() + 2);
		//		g.setColor(0xffffff);
		//		g.drawRect(getLeft(), getTop() + offsetHeight, getWidth(), getHeight());
		return super.paint(g, offsetHeight, isFocused);
		//		return false;
	}

	public void onChildHeightChange(UIElement childChangedHeight, int difference)
	{
		int index = getChildIndex(childChangedHeight);

		if (index >= 0)
		{
			UIElement uie = null;
			int totalHeight = 0;
			for (int i = 0, j = getChildrenCount(); i < j; i++)
			{
				uie = getChildAt(i);
				totalHeight += uie.getHeight();
				if (i > index)
				{
					uie.setTop(uie.getTop() + difference);
				}
			}

			setHeight(totalHeight);
			getParent().onChildHeightChange(this, difference);
		}
	}

	public int getCurLineHeight()
	{
		return m_curLineHeight;
	}

	public void setCurLineHeight(int curLineHeight)
	{
		m_curLineHeight = curLineHeight;
	}

	public int onPointerPress(int x, int y)
	{
		return super.onPointerPress(x, y);
	}

}
